package com.kll.tv.views

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.model.AccountModel
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.net.bean.GetAddressResponse
import com.kll.carolinehmibiz.net.bean.RefreshTokenResponse
import com.kll.carolinehmibiz.presentation.listener.MyHarvestAddressProtocol
import com.kll.carolinehmibiz.presentation.presenter.MyHarvestAddressPresenter
import com.kll.tv.base.views.BaseActivity
import com.kll.tv.databinding.ActivityMyHarvestAddressBinding
import com.kll.tv.utils.AppToast
import com.kll.tv.views.adapter.MyHarvestAddressAdapter

/**
 * 我的收货地址页
 */
class MyHarvestAddressActivity : BaseActivity<MyHarvestAddressPresenter>(),
    MyHarvestAddressProtocol.IView, MyHarvestAddressAdapter.OnAddressListItemClickListener {
    private lateinit var binding: ActivityMyHarvestAddressBinding
    private var data: ArrayList<GetAddressResponse> = arrayListOf()
    private var adapter: MyHarvestAddressAdapter? = null

    override fun initViewBinding(): View {
        binding = ActivityMyHarvestAddressBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun createPresenter(): MyHarvestAddressPresenter {
        return MyHarvestAddressPresenter(this, this)
    }

    override fun init() {
        // 初始化adapter
        if (adapter == null) {
            val addressListManager = LinearLayoutManager(this)
            addressListManager.orientation = LinearLayoutManager.VERTICAL
            binding.rvContent.layoutManager = addressListManager
            adapter = MyHarvestAddressAdapter(this)
            binding.rvContent.adapter = adapter
            adapter!!.setOnAddressListItemClickListener(this)
        }
        initData()
        initListener()
    }

    private fun initData() {
        mPresenter.getUserAddressList()
    }

    private fun initListener() {
        binding.ivBack.setOnClickListener {
            finish()
        }
        // 添加收货地址
        binding.tvAddAddress.setOnClickListener {
            startActivityForResult(Intent(this, AddressDetailActivity::class.java), 1)
        }
    }

    /**
     * 获取收货地址成功回调
     */
    override fun getUserAddressListSuccessed(data: ArrayList<GetAddressResponse>) {
        this.data = data
        adapter!!.data = this.data
    }

    /**
     * 获取收货地址失败回调
     */
    override fun getUserAddressListFailed(code: Int, message: String) {
        AppToast.show(message, 2000)
        if (code == 102) {
            // token过期 刷新token
            OperationManager.getInstancce().refreshToken(object : OperateListener {
                override fun onSuccess(state: Int, obj: Any?) {
                    var data = obj as RefreshTokenResponse
                    AccountModel.getInstance(baseContext).updateUserToken(data.token)
                    mPresenter.getUserAddressList()
                }

                override fun onFail(code: Int, message: String) {
                    AppToast.show(message, 2000)
                    if (code == 101) {
                        loginAgain()
                    }
                }

            })
        }
        if (code == 101) {
            loginAgain()
        }
    }

    /**
     * 点击地址列表item回调
     */
    override fun onAddressItemClick(data: GetAddressResponse) {
        val intent = Intent(this, AddressDetailActivity::class.java)
        val bundle = Bundle()
        bundle.putSerializable("data", data)
        intent.putExtra("data", bundle)
        intent.putExtra("isFrom", 1)
        startActivityForResult(intent, 1)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1)
        when (resultCode) {
            1 -> {
                // 重新刷新数据
                mPresenter.getUserAddressList()
            }
        }
    }

}